package com.qdsg.ysg.doctor.ui;

import android.annotation.SuppressLint;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.qdsg.ysg.doctor.util.ImageLoaderHelper;
import com.qdsg.ysg.doctor.util.ToastUtil;
import com.rest.business.RestProxy;
import com.rest.response.IMListResponse;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.BallPulseFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import butterknife.BindView;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import jz.joyoung.robot.util.UtilList;

import com.qdsg.ysg.doctor.R;
import com.qdsg.ysg.doctor.base.BaseActivity;

import org.jetbrains.annotations.NotNull;

/**
 * Created by HP on 2020/2/20.
 * 处方驳回列表页面
 */

public class RefuseListActivity extends BaseActivity {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.cl_empty)
    ConstraintLayout clEmpty;


    MyAdapter adapter;

    List<IMListResponse.IMItem> list = new ArrayList<>();


    @Override
    protected int getLayoutResource() {
        return R.layout.aty_base_list;
    }

    @Override
    protected void initViews() {
        tvTitle.setText("处方驳回");
        LinearLayoutManager manager = new LinearLayoutManager(RefuseListActivity.this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);
        adapter = new MyAdapter();
        recyclerView.setAdapter(adapter);

//        refreshLayout.setPrimaryColors(0xffffffff,  0xff5481FA);
        refreshLayout.setEnableLoadMore(false);
        refreshLayout.setOnRefreshListener(refreshlayout -> initData());
        refreshLayout.setOnLoadMoreListener(refreshlayout -> {

        });
    }

    @Override
    protected void initData() {
        queryIMList();
    }

    @Override
    protected void onResume() {
        super.onResume();
        queryIMList();
    }

    //    @OnClick(R.id.btn_history)
//    void btn_add() {
//        startActivity(RefuseListActivity.class);
//    }

    class MyAdapter extends RecyclerView.Adapter<MyAdapter.Holder> {

        @NotNull
        @Override
        public MyAdapter.Holder onCreateViewHolder(@NotNull ViewGroup parent, int viewType) {
            MyAdapter.Holder myHolder;
            myHolder = new MyAdapter.Holder(LayoutInflater.from(RefuseListActivity.this).inflate(R.layout.item_im, parent, false));
            return myHolder;
        }

        @SuppressLint("SetTextI18n")
        @Override
        public void onBindViewHolder(MyAdapter.Holder holder, final int position) {
            holder.itemView.setOnClickListener(v -> {
                Bundle bundle = new Bundle();
                bundle.putSerializable("diagnoseId", list.get(position).diagnoseId);
                bundle.putSerializable("prescriptionId", list.get(position).id);
                startActivityForResult(AddRefusePrescriptionActivity.class, bundle, 10000);
            });
            holder.tv_name.setText("患者：" + list.get(position).patientName);
            holder.tv_department.setVisibility(View.GONE);
            holder.tv_message.setText("驳回医生：" + list.get(position).auditor);
            holder.tv_time.setText(list.get(position).passTime);
            holder.tv_unreadCount.setVisibility(View.GONE);
            ImageLoaderHelper.getInstance().GlideImageLoader(mContext, list.get(position).picture, holder.img_head, R.mipmap.img_default);
        }

        @Override
        public int getItemCount() {
            return list.size();
        }

        class Holder extends RecyclerView.ViewHolder {

            TextView tv_name, tv_department, tv_message, tv_time, tv_unreadCount;
            ImageView img_head;

            public Holder(View itemView) {
                super(itemView);
                tv_name = itemView.findViewById(R.id.tv_name);
                tv_time = itemView.findViewById(R.id.tv_time);
                tv_department = itemView.findViewById(R.id.tv_department);
                tv_message = itemView.findViewById(R.id.tv_message);
                img_head = itemView.findViewById(R.id.img_head);
                tv_unreadCount = itemView.findViewById(R.id.tv_unreadCount);
            }
        }
    }


    private void queryIMList() {
        RestProxy.getInstance().getRefusePrescription(new Observer<IMListResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(IMListResponse imListResponse) {
                if (!UtilList.isEmpty(imListResponse.data.records)) {
                    refreshLayout.setVisibility(View.VISIBLE);
                    clEmpty.setVisibility(View.GONE);
                    list.clear();
                    list.addAll(imListResponse.data.records);
                    adapter.notifyDataSetChanged();
                } else {
                    refreshLayout.setVisibility(View.GONE);
                    clEmpty.setVisibility(View.VISIBLE);
                }

            }

            @Override
            public void onError(Throwable e) {
                ToastUtil.onError(mContext, e);
                refreshLayout.finishRefresh(false);
            }

            @Override
            public void onComplete() {
                refreshLayout.finishRefresh(true);
            }
        });
    }


}
